%Read level2 residual fields
%%%%%%%%%%%% 3D results %%%%%%%%%%%%%%%%%%%%%
file = 'BiscayResidual.nc';
u = nc_varget(file,'Vel_X');
v = nc_varget(file,'Vel_Y');
lon = nc_varget(file,'lon');
lat = nc_varget(file, 'lat');
depth = nc_varget(file, 'depth');
level = nc_varget(file, 'Lev_Z');

%Create level1 residual fields
out = 'MOHIDresidual3D.nc';
nc_create_empty(out, nc_write_mode);
nc_add_dimension(out, 'lon', length(lon));
nc_add_dimension(out, 'lat', length(lat));
nc_add_dimension(out, 'deptht', length(depth));

s_ncvars = struct ( 'Name', ...
                        {   'lon',...
                            'lat',...
                            'deptht',...
                            'u',...
                            'v',...
                            'elevation'}, ...
                   'Nctype', ...
                        {   'float',...
                            'float',...
                            'float',...
                            'float',...
                            'float',...
                            'float'}, ...
                   'Dimension', ...
                        {   {'lon'},...
                            {'lat'},...
                            {'deptht'},...
                            {'deptht' 'lat' 'lon'},...
                            {'deptht' 'lat' 'lon'},...
                            {'lat' 'lon'}}, ...
                   'Attribute', ...
                        {   struct( 'Name','units',...
                                    'Value','degrees_east' ), ...
                            struct( 'Name','units',...
                                    'Value','degrees_north' ), ...
                            struct( 'Name','units',...
                                    'Value','m' ), ...
                            struct( 'Name','units',...
                                    'Value','m s-1' ), ...
                            struct( 'Name','units',...
                                    'Value','m s-1' ), ...
                            struct( 'Name','units',...
                                    'Value','m' ), ...
                         })

for i=1:length(s_ncvars)                     
    nc_addvar(out, s_ncvars(i));
end

nc_dump(out);